import {Injectable} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd/message';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Config} from './config';
import {Router} from '@angular/router';
import axios from 'axios';
import qs from 'qs';

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.withCredentials = true;

@Injectable()
export class Http {
  constructor(public msg: NzMessageService, public httpClient: HttpClient, private router: Router) {

  }

  public async get(url: string, data: object) {
    const result: object = await axios.get(Config.API_URL + url, {
      params: {
        ...data
      },
      headers: {
        'content-type': 'application/x-www-form-urlencoded'
      }
    });
    if (result.data.msg === '您没有权限访问当前地址') {
      this.msg.error(result.data.msg);
    }
    if (result.data.msg === '请您登录') {
      this.msg.error('Session过期，请重新登录');
      this.router.navigate(['login']);
    }
    return result;
  }

  public async post(url: string, data: object) {
    const d = qs.stringify(data);
    const result: object = await axios.post(Config.API_URL + url, d, {
      headers: {'content-type': 'application/x-www-form-urlencoded'},
    });
    if (result.data.msg === '您没有权限访问当前地址') {
      this.msg.error(result.data.msg);
    }
    if (result.data.msg === '请您登录') {
      this.msg.error('Session过期，请重新登录');
      this.router.navigate(['login']);
    }
    return result;
  }

  public async put(url: string, data: object) {
    // put 有问题，参数不能在url上面。
    let str = '?';
    for (let key in data) {
      str += `${key}=${data[key]}&`;
    }
    const result: object = await axios.put(Config.API_URL + url + str);
    if (result.data.msg === '您没有权限访问当前地址') {
      this.msg.error(result.data.msg);
    }
    if (result.data.msg === '请您登录') {
      this.msg.error('Session过期，请重新登录');
      this.router.navigate(['login']);
    }
    return result;
  }

  public async delete(url: string, data: object) {
    const result: object = await axios.delete(Config.API_URL + url, {
      params: {
        ...data
      },
      headers: {
        'content-type': 'application/x-www-form-urlencoded'
      }
    });
    if (result.data.msg === '您没有权限访问当前地址') {
      this.msg.error(result.data.msg);
    }
    if (result.data.msg === '请您登录') {
      this.msg.error('Session过期，请重新登录');
      this.router.navigate(['login']);
    }
    return result;
  }

  public upload(url: string, param: object, callbackFun) {
    axios
      .post(Config.API_URL + url, param, {
        headers: {
          'Content-Type': 'application/json',
          // 'token': localStorage.getItem('token')
        },
      })
      .then((result) => {
        return callbackFun(result.data);
      })
      .catch((error) => {
        return callbackFun(error.data);
      });
  }
}

interface ValueObject {
  status?: number;
}
